package com.kly.chatter.api.application.controller.audit;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.fqgj.common.api.Page;
import com.fqgj.common.api.Response;
import com.kly.chatter.api.application.controller.audit.req.ProfileAuditReq;
import com.kly.chatter.api.application.controller.audit.req.ProfileListReq;
import com.kly.chatter.api.application.controller.audit.req.ProfileResponse;
import com.kly.chatter.api.application.controller.audit.req.ProfileVo;
import com.kly.chatter.api.application.dto.ChatterModelInfoExamine;
import com.kly.chatter.api.application.service.ProfileAuditService;
import com.kly.chatter.api.application.utils.Result;
import com.kly.sc.api.application.annos.SignIgnore;
import com.kly.sc.api.application.annos.VisitorAccessible;
import com.kly.sc.api.application.helper.RequestLocalInfo;
import com.kly.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/profile")
public class ProfileAuditController {

    @Resource
    ProfileAuditService profileAuditService;
    @Resource
    UserService userService;

    /**
     * Profile审核列表
     */
    @SignIgnore
    @VisitorAccessible
    @RequestMapping("/audit/list")
    @SentinelResource
    public Result<ProfileResponse> profileList(@RequestBody ProfileListReq req) {

        final int totalNum = profileAuditService.getProfileAuditCnt(req.getChatterAccount(), req.getModelUserCode(), req.getApprovalStatus());
        if (totalNum == 0) {
            return Result.ok(ProfileResponse.builder().totalNum(0).build());

        }

        final List<ChatterModelInfoExamine> chatterModelInfoExamines = profileAuditService.listProfileAudit(req.getChatterAccount(), req.getModelUserCode(), req.getApprovalStatus(), new Page(req.getPageSize(), req.getCurrent()));
        final List<ProfileVo> userProfileAudits = chatterModelInfoExamines.stream().map(o -> {
            ProfileVo vo = new ProfileVo();
            BeanUtils.copyProperties(o, vo);
            vo.setApprovalStatus(o.getOperateStatus());
            vo.setModelUserCode(o.getModelUserCode());
            vo.setChatterNickName(o.getChatterNickName());
            vo.setDisableFlag(o.getOperateStatus() == 2 || o.getOperateStatus() == 3);

            return vo;
        }).collect(Collectors.toList());

        return Result.ok(ProfileResponse.builder().totalNum(totalNum).profileList(userProfileAudits).build());
    }

    /**
     * Profile审核
     */
    @SignIgnore
    @VisitorAccessible
    @RequestMapping("/audit")
    @SentinelResource
    public Result profileAudit(@RequestBody ProfileAuditReq req) {
        if (req.getId() == null) {
            return Result.build(500, "The id should not be empty");
        }
        if (req.getAuditStatus() == null) {
            return Result.build(500, "The approvalStatus should not be empty");
        }
        final Response<ChatterModelInfoExamine> approvalResult = profileAuditService.approval(req.getId(), req.getAuditStatus(), req.getAccount());
        if (approvalResult == null || approvalResult.getCode() != 0) {
            return Result.build(500, "Audit Failed");
        }

        // 审核通过，同步到用户库
        log.info("profileAudit, reqStr:{}", req.getAuditStatus() == 2);
        if (req.getAuditStatus() == 2) {
            final ChatterModelInfoExamine chatterModelInfoExamine = approvalResult.getData();
            // type = 1: 更新aboutMe信息
            userService.updateUserProfile(chatterModelInfoExamine.getModelUserCode(), 1, chatterModelInfoExamine.getUserInfo());
        }

        return Result.ok();
    }
}